%------------------------------------------------------------
% Author: vjache
% Created: 19.11.2009
% Description: TODO: Add description to parse_sparql
%------------------------------------------------------------
-module(parse_sparql).

%------------------------------------------------------------
% Include files
%------------------------------------------------------------
-include("../include/pipe.hrl").
-include("../include/parser.hrl").
%------------------------------------------------------------
% Exported Functions
%------------------------------------------------------------
-export([]).

-record(state,{chars,pipe}).

%------------------------------------------------------------
% API Functions
%------------------------------------------------------------

parse(Chars,Pipe) when is_list(Chars) ->
	State=#state{chars=Chars,pipe=pipe:open(Pipe)},
	State1=parse_prefixes(skip_wsp(State)),
	State2=parse_query_form(State1),
	State3=parse_pattern(State2),
	State4=parse_range(State3),
	State5=parse_order(State4),
	ok.

%------------------------------------------------------------
% Local Functions
%------------------------------------------------------------

parse_prefixes(S=#state{chars=[$P,$R,$E,$F,$I,$X,Wsp|T],pipe=_}) 
	when ?IS_WITE_SPACE(Wsp) ->
	S1=parse_pns(S#state{chars=T}),
	S2=parse_iri(S1),
	parse_prefixes(skip_wsp(S2));
parse_prefixes(S)->
	S.

skip_wsp(S=#state{chars=[Wsp|T],pipe=_}) when ?IS_WITE_SPACE(Wsp) ->
	skip_wsp(S#state{chars=T});
skip_wsp(S=#state{chars=[$#|T],pipe=_}) ->
	skip_comment(S#state{chars=T});
skip_wsp(S) ->
	S.

skip_comment(S=#state{chars=[?NL|T],pipe=_}) ->
	skip_wsp(S#state{chars=T});
skip_comment(S=#state{chars=[_|T],pipe=_}) ->
	skip_comment(S#state{chars=T}).
